import winreg
from tkinter import *
from tkinter.ttk import *

def enableAway():
    global label
    hKey = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE,r'SYSTEM\CurrentControlSet\Control\Session Manager\Power', \
    	reserved=0, access=winreg.KEY_SET_VALUE)
    winreg.SetValueEx(hKey, 'AwayModeEnabled', 0,winreg.REG_DWORD,1)
    winreg.CloseKey(hKey)
    label['text'] = query()
    
def disableAway():
    global label
    hKey = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE,r'SYSTEM\CurrentControlSet\Control\Session Manager\Power', \
    	reserved=0, access=winreg.KEY_SET_VALUE)
    winreg.SetValueEx(hKey, 'AwayModeEnabled', 0,winreg.REG_DWORD,0)
    winreg.CloseKey(hKey)
    label['text'] = query()
    
def query():
    hKey = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE,r'SYSTEM\CurrentControlSet\Control\Session Manager\Power', \
        reserved=0, access=winreg.KEY_QUERY_VALUE)
    try:
        v,t = winreg.QueryValueEx(hKey, 'AwayModeEnabled')
    except:
        return '当前状态：没有设置'
    finally:
        winreg.CloseKey(hKey)
    if v==1:
        return '当前状态：打开'
    else:
        return '当前状态：关闭'
    

top = Tk()
top.resizable(False,False)
top.title("离开模式")

curWidth = 400#top.winfo_reqwidth() # get current width
curHeight = 300#top.winfo_height() # get current height
scnWidth,scnHeight = top.maxsize() # get screen width and height
tmpcnf = '%dx%d+%d+%d'%(curWidth,curHeight,(scnWidth-curWidth)/2,(scnHeight-curHeight)/2)
top.geometry(tmpcnf)

btn1 = Button(top, text='打开离开模式', command=enableAway)
btn2 = Button(top, text='关闭离开模式', command=disableAway)
label = Label(top,text=query())
label.pack()
btn1.pack()
btn2.pack()



top.mainloop()
